package com.css.fxfzypg.modules.suggestinfo.analysis.emergencydisaster.repository.impl;

import com.css.fxfzypg.modules.suggestinfo.analysis.emergencydisaster.repository.EmergencyDisasterRepository;
import com.css.fxfzypg.modules.suggestinfo.analysis.emergencydisaster.repository.entity.EmergencyDisasterEntity;
import com.css.fxfzypg.modules.suggestinfo.analysis.suggestionanalysis.vo.ParamsVo;
import com.css.fxfzypg.util.PlatformObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class EmergencyDisasterRepositoryImpl implements EmergencyDisasterRepository {

    @Autowired
    JdbcTemplate jdbcTemplate;
    @Autowired
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public Boolean save(EmergencyDisasterEntity emergencyDisasterEntity) {
        String sql = "insert into ypg_emergency_disaster values(:id,:taskNum,:province,:provinceId,:city,:cityId,:area,:areaId," +
                ":emergencySystem,:emergencyWorkingMechanism,:emergencySuppliesReserve,:emergencyRescueForce,:emergencyShelter," +
                ":supportability,:rumorEmergencyCapacity,:popularization,:otherMeasures," +
                ":divisionFlag,:delFlag,:createName,:createId,:createTime,:maintainer,:maintainerId,:maintainTime)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(emergencyDisasterEntity);
        return namedParameterJdbcTemplate.update(sql, source) > 0;
    }

    @Override
    public Boolean update(EmergencyDisasterEntity emergencyDisasterEntity) {
        String sql = "update ypg_emergency_disaster set emergency_system=:emergencySystem,emergency_working_mechanism=:emergencyWorkingMechanism,emergency_supplies_reserve=:emergencySuppliesReserve,emergency_rescue_force=:emergencyRescueForce,emergency_shelter=:emergencyShelter," +
                "supportability=:supportability,rumor_emergency_capacity=:rumorEmergencyCapacity,popularization=:popularization,other_measures=:otherMeasures," +
                "maintainer=:maintainer,maintainer_id=:maintainerId,maintain_time=:maintainTime where id = '"+emergencyDisasterEntity.getId()+"'";
        SqlParameterSource source = new BeanPropertySqlParameterSource(emergencyDisasterEntity);
        return namedParameterJdbcTemplate.update(sql, source) > 0;
    }

    @Override
    public EmergencyDisasterEntity getEmergencyDisasterData(ParamsVo params) {
        try{
            StringBuilder sql = new StringBuilder("select * from ypg_emergency_disaster ");
            StringBuilder whereSql = new StringBuilder("where del_flag = '0' and task_num = '"+params.getTaskNum()+"'");
            if(!PlatformObjectUtils.isEmpty(params.getProvince())){
                whereSql.append(" and province_id = '"+ params.getProvinceId()+"'");
            }
            if(!PlatformObjectUtils.isEmpty(params.getCity())){
                whereSql.append(" and city_id = '"+ params.getCityId()+"'");
            }
            String flag = params.getFlag();
            if(flag.equals("1")){
                whereSql.append(" and division_flag = '1'");
            }else if (flag.equals("0")){
                if(!PlatformObjectUtils.isEmpty(params.getArea())){
                    whereSql.append(" and area_id = '"+ params.getAreaId()+"'");
                }
                whereSql.append(" and division_flag = '0'");
            }
            EmergencyDisasterEntity emergencyDisasterEntity = jdbcTemplate.queryForObject(sql.append(whereSql).toString(), new BeanPropertyRowMapper<>(EmergencyDisasterEntity.class));
            return emergencyDisasterEntity;
        }catch (Exception e){
            return null;
        }
    }

    @Override
    public List<EmergencyDisasterEntity> getEmergencyDisasterDataByTaskNum(String taskNum) {
        StringBuilder sql = new StringBuilder("select * from ypg_emergency_disaster ");
        StringBuilder whereSql = new StringBuilder("where del_flag = '0' and task_num = '"+taskNum+"'");
        List<EmergencyDisasterEntity> emergencyDisasterEntities = jdbcTemplate.query(sql.append(whereSql).toString(), new BeanPropertyRowMapper<>(EmergencyDisasterEntity.class));
        return emergencyDisasterEntities;
    }

    @Override
    public EmergencyDisasterEntity getEmergencyDisasterDataByTaskNum(String taskNum, String cityId, String areaId) {
        StringBuilder sql = new StringBuilder("select * from ypg_emergency_disaster ");
        StringBuilder whereSql = new StringBuilder("where del_flag = '0' and task_num = '"+taskNum+"' and city_id = '"+cityId+"'  and area_id = '"+areaId+"' and division_flag = '0'");
        try {
            EmergencyDisasterEntity emergencyDisposalEntity = jdbcTemplate.queryForObject(sql.append(whereSql).toString(), new BeanPropertyRowMapper<>(EmergencyDisasterEntity.class));
            return emergencyDisposalEntity;
        }catch (EmptyResultDataAccessException e){
            return null;
        }
    }

    @Override
    public int getCountById(String id) {
        int count = jdbcTemplate.queryForObject("select count(*) count from ypg_emergency_disaster where id = '"+id+"' ", Integer.class);
        return count;
    }

    @Override
    public List<EmergencyDisasterEntity> getEmergencyDisasterAreaData(ParamsVo paramsVo) {
        StringBuilder sql = new StringBuilder("select * from ypg_emergency_disaster ");
        StringBuilder whereSql = new StringBuilder("where del_flag = '0' and division_flag = '0' and task_num = '"+paramsVo.getTaskNum()+"' and province_id = '"+paramsVo.getProvinceId()+"' and city_id = '"+paramsVo.getCityId()+"'");
        List<EmergencyDisasterEntity> emergencyDisasterEntities = jdbcTemplate.query(sql.append(whereSql).toString(), new BeanPropertyRowMapper<>(EmergencyDisasterEntity.class));
        return emergencyDisasterEntities;
    }
}
